import std/os
import std/parsecfg
import strutils

const cfg = "data.ini"
var dict = newConfig()
var cmd = commandLineParams()
var command = cmd[0]

proc add(p_video_name:string) = 
    if not fileExists(cfg):
        var file = open(cfg,fmWrite)
        dict.setSectionKey(p_video_name,"status","在看")
        dict.setSectionKey(p_video_name,"at","0")
        file.write(dict)
        file.close()
    else:
        var dict = loadConfig(cfg)
        dict.setSectionKey(p_video_name,"status","在看")
        dict.setSectionKey(p_video_name,"at","0")
        writeConfig(dict,cfg)


proc xiugai(p_video_name:string,p_status:string,p_at:string) = 
    var dict = loadConfig(cfg)
    dict.setSectionKey(p_video_name,"status",p_status)
    dict.setSectionKey(p_video_name,"at",p_at)
    writeConfig(dict,cfg)

proc fs_del(p_video_name:string) = 
    var dict = loadConfig(cfg)
    delSectionKey(dict,p_video_name,"status")
    delSectionKey(dict,p_video_name,"at")
    #.delSectionKey(p_video_name)
    dict.writeConfig(cfg)
# 想看 在看 看过 抛弃
var want = 0
var in_watch = 0
var watched = 0
var paoqi = 0

proc getInfo() = 
    var c = open(cfg,fmRead)
    var f = open("统计.txt",fmWrite)
    var s = c.readAll()
    want = s.count("想看")
    in_watch = s.count("在看")
    watched = s.count("看过")
    paoqi = s.count("抛弃")
    f.writeLine("想看:"& $want)
    f.writeLine("在看:"& $in_watch)
    f.writeLine("看过:"& $watched)
    f.writeLine("抛弃:"& $paoqi)
    c.close()
    f.close()

proc test() = 
    var file = open("data.ini",fmWrite)
    dict.setSectionKey("test","a","a")
    dict.setSectionKey("test2","b","b")
    file.write(dict)
    file.close()

if command == "add":
    add(cmd[1])
elif command == "xiugai":
    xiugai(cmd[1],cmd[2],cmd[3])
elif command == "test":
    test()
elif command == "del":
    fs_del(cmd[1])
elif command == "info":
    getInfo()